﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using Newtonsoft.Json;
using System.Web.Security;
using ZLCMS.Business.Util;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Control.FileControl
{
    public partial class BindUrl : System.Web.UI.Page
    {
        protected int _id;
        protected long _size;
        protected string _name;
        protected string _path;
        protected FrameHelper _framePage;

        protected string _haveFile;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);

            _haveFile = "0";

            if (IsPostBack)
            {
                string urlPath = txtName.Text.Replace('\\', '/');

                if (!urlPath.StartsWith("/"))
                {
                    urlPath = "/" + urlPath;
                }
               
                string serverPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + urlPath;

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(serverPath);

                if (!fileInfo.Exists)
                {
                    new FrameHelper(Page).Alert(new ReturnCode() { statusCode = 300, message = "请输入正确的文件地址!" });
                    return;
                }
                else
                {
                    _name = System.IO.Path.GetFileName(serverPath);
                    _size = fileInfo.Length;
                    _path = urlPath;

                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        ZLCMS.Data.Model.FileInfo file = new ZLCMS.Data.Model.FileInfo();
                        file.FilePath = _path;
                        file.DownloadCount = 0;
                        file.UploadMen = UserKit.GetUserAccountFailLogin();
                        file.UploadTime = DateTime.Now;
                        file.FileSize = int.Parse(_size.ToString());
                        file.FileName = _name;

                        session.Save(file);
                        session.Flush();

                        _id = file.Id;

                        _haveFile = "1";
                    }
                }
            }
        }
        
    }
}
